﻿

Imports System.Data
Imports VSModel
Imports Cls_Common
Imports SecurityService

Partial Class Control_BanHang_ContentEditor
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "Sub and Function "
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then

            If Not Request.QueryString("Id") Is Nothing Then
                hidID.Value = Request.QueryString("Id")
            End If


            btnDelete.Attributes.Add("onclick", "return ComfirmDialog('Bạn có chắc chắn muốn xóa không?',0,'" + btnDelete.ClientID + "','" + hidID.Value.ToString + "',1);")


            'active tab tuong ung voi Page
            'InVisble button ko can thiet
            If Request.PhysicalPath.Contains("List.aspx") Then
                lbtList.CssClass = "current_manage"
                lbtList.Text = "<span class='current_manage'>Quản lý</span>"
                lbtEdit.Visible = False
                lbtView.Visible = False
                btnDelete.Visible = False


            ElseIf Request.PhysicalPath.Contains("Create.aspx") Then

                lbtAdd.CssClass = "current_manage"
                lbtAdd.Text = "<span class='current_manage'>Thêm mới</span>"
                lbtEdit.Visible = False
                lbtView.Visible = False
                btnDelete.Visible = False

                If Not CType(hidCan_Add_Edit.Value, Boolean) Then
                    Excute_Javascript("AlertCutOff();", Me.Page, True)
                End If
            ElseIf Request.PhysicalPath.Contains("Detail.aspx") Then
                lbtView.CssClass = "current_manage"
                lbtView.Text = "<span class='current_manage'>Xem</span>"
                'Permission

            ElseIf Request.PhysicalPath.Contains("Edit.aspx") Then

                lbtEdit.CssClass = "current_manage"
                lbtEdit.Text = "<span class='current_manage'>Sửa</span>"
                'Permission



            End If

        End If
    End Sub


    Protected Sub lbtList_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbtList.Click
        Response.Redirect("List.aspx")
    End Sub
    Protected Sub lbtAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbtAdd.Click
        Response.Redirect("Create.aspx")
    End Sub
    Protected Sub lbtEdit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbtEdit.Click
        Response.Redirect("Edit.aspx?Id=" & hidID.Value & "")
    End Sub
    Protected Sub lbtView_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbtView.Click
        Response.Redirect("Detail.aspx?Id=" & hidID.Value & "")
    End Sub
    Protected Sub lnkbtnDelete_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnDelete.Click
        Dim intId As Integer
        Dim strLogName As String = ""
        Using data As New VSEntities
            intId = hidID.Value
            'add DomainName Duc 08/09/2014
            Dim q = (From p In data.BanHangs Where p.Domain = DomainName And p.Id = intId Select p).FirstOrDefault
            If Not IsNothing(q) Then

                Try
                    'delete BanHangdetail
                    'add DomainName Duc 08/09/2014
                    Dim _od = (From k In data.BanHangDetails Select k Where k.Domain = DomainName And k.BanHangId = q.Id).ToList
                    For Each a In _od
                        data.BanHangDetails.DeleteObject(a)
                    Next

                    strLogName = q.Id
                    data.BanHangs.DeleteObject(q)
                    data.SaveChanges()
                    Insert_App_Log("Delete BanHang:" & strLogName & "", Function_Name.Banhang, Audit_Type.Delete, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                    Excute_Javascript("Alertbox('Xóa dữ liệu thành công.');window.location ='../../Page/BanHang/List.aspx';", Me.Page, True)
                Catch ex As Exception
                    log4net.Config.XmlConfigurator.Configure()
                    log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                    Excute_Javascript("Alertbox('Xóa thất bại.');window.location ='../../Page/BanHang/List.aspx';", Me.Page, True)
                End Try
            End If
        End Using
    End Sub

#End Region
End Class
